package com.ruoyi.framework.config;

import com.ruoyi.framework.config.properties.StorageProperties;
import com.ruoyi.framework.storage.*;
import io.minio.MinioClient;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(StorageProperties.class)
@RequiredArgsConstructor
public class StorageConfig {
    private final StorageProperties properties;

    @Bean
    public StorageService storageService() {
        Storage storage = this.minioStorage();

        return new StorageService(storage);
    }

    public MinioStorage minioStorage() {
        StorageProperties.Minio minio = properties.getMinio();

        MinioClient minioClient = MinioClient.builder()
                .endpoint(minio.getEndpoint())
                .credentials(minio.getAccessKeyId(), minio.getAccessKeySecret())
                .build();

        MinioStorage minioStorage = new MinioStorage();
        minioStorage.setPrefixUrl(minio.getPrefixUrl());
        minioStorage.setEndpoint(minio.getEndpoint());
        minioStorage.setBucketName(minio.getBucketName());
        minioStorage.setMinioClient(minioClient);
        return minioStorage;
    }
}
